{% extends 'pendulum/pendulum_base.html' %}
{% load pendulum_tags %}

{% block title %}Pendulum Entries{% endblock %}

{% block content %}
<h2>Pendulum Entries</h2>

{{ block.super }}

<table id="entry-table">
    <tr>
        <th colspan="7">
            <a href="{% url pendulum-previous-entries previous_period %}" title="Go to the previous period">&laquo;</a>
            {{ period.0|date:"N j, Y" }} -
            {{ period.1|date:"N j, Y" }}
            {% if has_next %}
            <a href="{% url pendulum-previous-entries next_period %}" title="Go to the next period">&raquo;</a>
            {% else %}
            &raquo;
            {% endif %}
        </th>
    </tr>
    <tr>
        <th>Date</th>
        <th>Start Time</th>
        <th>End Time</th>
        <th>Hours</th>
        <th>Project</th>
        <th colspan="2">Activity</th>
    </tr>
    {% for entry in entries %}
    {% with entry.start_time|date:"W" as week_of_year %}
    {% ifchanged week_of_year %}{% week_totals entry.start_time.date %}{% endifchanged %}
    {% endwith %}
    {% ifchanged entry.start_time.date %}{% day_totals entry.start_time.date %}{% endifchanged %}
    <tr class="{% cycle odd,even as rowclass %}">
        <td class="entry-date">
            {% if entry.is_closed %}<a href="{% url pendulum-update entry.id %}" title="Update this entry">{% endif %}{{ entry.start_time|date:"j N Y" }}{% if entry.is_closed %}</a>{% endif %}
        </td>
        <td class="entry-start">{{ entry.start_time|date:"P" }}</td>
        <td class="entry-end">{% if entry.end_time %}
            {{ entry.end_time|date:"P" }}{% else %}
            <a href="{% url pendulum-clock-out entry.id %}">Clock Out</a>
        {% endif %}</td>
        <td class="entry-hours">{% if entry.end_time %}
            {{ entry.total_time }}<br />
            <span class="hours">({{ entry.hours }})</span>{% else %}
            {% if entry.is_paused %}
            <a href="{% url pendulum-toggle-paused entry.id %}">Unpause</a>
            {% else %}
            <a href="{% url pendulum-toggle-paused entry.id %}">Pause</a>
            {% endif %}
        {% endif %}</td>
        <td class="entry-project">{{ entry.project }}</td>
        <td class="entry-activity">
            {% if entry.activity.code %}
            {{ entry.activity.code }}
            {% else %}
            ...
            {% endif %}
        </td>
        <td class="entry-admin">
            {% if entry.is_closed %}<a href="{% url pendulum-update entry.id %}">&delta;</a>{% endif %}
            <a href="{% url pendulum-delete entry.id %}">&times;</a>
        </td>
    </tr>
    {% if entry.seconds_paused %}<tr class="{{ rowclass }} entry-paused">
        <td colspan="2">&nbsp;</td>
        <th>Breaks:</th>
        <td class="entry-paused">{{ entry.paused_time }}</td>
        <td colspan="3">&nbsp;</td>
    </tr>{% endif %}
    {% if entry.comments %}<tr class="{{ rowclass }}">
        <td colspan="7" class="entry-comments">{{ entry.comments }}</td>
    </tr>{% endif %}
    {% endfor %}
</table>

{% if entries %}
<table class="entry-statistics">
    <tr>
        <th colspan="2">Statistics</th>
    </tr>
    <tr>
        <th>Projects</th>
        <th>Activities</th>
    </tr>
    <tr>
        <td>
        {% entries_projects entries as projects %}
        {% for project in projects %}
        {% if forloop.first %}<table>{% endif %}
            <tr class="{% cycle odd,even as rowclass %}">
                <th>{{ project.name }}</th>
                <td>
                    {% time_in_period project period %}
                    ({% filter floatformat:2 %}{% hours_in_period project period %}{% endfilter %})
                </td>
            </tr>
        {% if forloop.last %}</table>{% endif %}
        {% endfor %}
        </td>
        <td>
        {% entries_activities entries as activities %}
        {% for activity in activities %}
        {% if forloop.first %}<table>{% endif %}
            <tr class="{% cycle odd,even as rowclass %}">
                <th>{{ activity.name }}</th>
                <td>
                    {% time_in_period activity period %}
                    ({% filter floatformat:2 %}{% hours_in_period activity period %}{% endfilter %})
                </td>
            </tr>
        {% if forloop.last %}</table>{% endif %}
        {% endfor %}
        </td>
    </tr>
    <tr class="odd">
        <th>Total Hours</th>
        <td>
            {% total_time_for_period period user %}
            ({% filter floatformat:2 %}{% total_hours_for_period period user %}{% endfilter %})
        </td>
    </tr>
    <tr class="even">
        <th>Total Entries</th>
        <td>{{ entries.count }}</td>
    </tr>
</table>
{% endif %}

{% endblock %}
